package com.jxchexie.admin;

import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.jxchexie.model.Admin;
import com.jxchexie.signin.R;
import com.jxchexie.utils.JsonUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import org.json.JSONObject;

import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Response;

import static com.jxchexie.api.Api.CHECK_ADMIN;

public class AdminLoginActivity extends AppCompatActivity {
    private Toolbar SignRecordToolbar;
    private EditText adminUserTextView;
    private EditText adminPwdTextView;
    private ImageView adminLoginBtn;
    private String admin_user;
    private String admin_pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin_login);
        initView();
        initEvent();
    }

    private void initEvent() {
        //
        SignRecordToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AdminLoginActivity.this.finish();/*关闭当前页*/
            }
        });
        //
        adminLoginBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                HashMap<String,String> params = new HashMap();
                admin_user = adminUserTextView.getText().toString();
                admin_pwd = adminPwdTextView.getText().toString();
                params.put("admin_user",admin_user);
                params.put("admin_pwd",admin_pwd);
                JSONObject jsonObject = new JSONObject(params);
                OkGo.post(CHECK_ADMIN)
                        .upJson(jsonObject.toString())
                        .execute(new StringCallback(){
                            @Override
                            public void onSuccess(String s, Call call, Response response) {
                                Admin admin = new Admin();
                                admin = JsonUtils.fromJson(s,Admin.class);
                                if (admin.getStatus().equals("success")){
                                    Toast.makeText(AdminLoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                                    startActivity(new Intent(AdminLoginActivity.this, AdminActivity.class));
                                    //挑战后就将用户名密码清空，防止泄露安全
                                    adminUserTextView.setText("");
                                    adminPwdTextView.setText("");

                                }else {
                                    Toast.makeText(AdminLoginActivity.this,admin.getMsg(),Toast.LENGTH_SHORT).show();
                                }
                            }

                            @Override
                            public void onError(Call call, Response response, Exception e) {
                                Toast.makeText(AdminLoginActivity.this,"系统或网络错误登陆失败",Toast.LENGTH_SHORT).show();
                            }
                        });

            }
        });
    }

    private void initView() {
        adminUserTextView = (EditText)findViewById(R.id.adminUserTextView);
        adminPwdTextView = (EditText)findViewById(R.id.adminPwdTextView);
        adminLoginBtn = (ImageView)findViewById(R.id.adminLoginBtn);
        //返回按钮
        SignRecordToolbar=(Toolbar)this.findViewById(R.id.SignRecordToolbar);

    }
}